// pages/activity/nationalDay/nationalDay.js
import {
  getFlowInfo,
  takeCoupon,
  takeOctoberRank,
  getCouponGetStatus
} from '../../../api/activity'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isInNationalTest: getApp().globalData.isInNationalTest,
    CustomBar: getApp().globalData.CustomBar,
    activeTab: 0,
    hasTake: true,
    rawardList: [],
    signDialogVisible: false, //签到弹框
    ruleDialogVisible: false, // 规则弹框
    awardDialogVisible: false, // 奖品弹框
    rankList: [],
    showCount: 5,
    canShowMore: false, //是否显示更多
    jsScrolling: false,
    tabTop: 2659,
    stickyTop: 0,
    enable: true,
    showCount: 5,
    canShare: false,
    myPersonalData1: {
      type: 'first',
      all_flow: '',
      avator: '',
      userId: '',
    },
    myPersonalData2: {
      type: 'second',
      invite_key: '',
      emcee_name: '',
      real_name: '',
      avator: '',
      gap_like: '',
      end_like: '',
      size: '',
      lottery_money: '',
    },
    awardObj: {
      list1: [],
      list2: [],
      list3: [],
      list4: [],
      maxInfo: {
        all_flow: '',
        avator: '',
        userId: '',
      }
    },
    optionsTab: '',
  },

  onShow(){
    this.setData({
      isInNationalTest: getApp().globalData.isInNationalTest
    })
    if (getApp().globalData.tab==1) {
      getApp().globalData.tab==0
      this.changeTab(1)
    } else {
      const date = new Date();
      const month = date.getMonth() + 1
      const day = date.getDate()
      if (month === 10 && day > 8) {
        // this.activeTab = 1;
        this.changeTab(1)
        console.log('已经到第二个活动时间了')
      }
    }
    this.initOctoberRank()
    this.initFlowInfo();

  },
  closeSignBox() {
    this.setData({
      signDialogVisible: false
    })
  },
  onCVPageScroll(e) {
    // console.log(this.data.jsScrolling , e.detail)
    if (this.data.jsScrolling) {
      return;
    }
    // console.log('滑动中' ,e)
    const {
      scrollTop
    } = e.detail

    console.log( scrollTop, this.data.tabTop  )

    if (scrollTop > this.data.tabTop - 540) {
      // this.activeTab = 1
      this.setData({
        activeTab: 1,
      })
    } else {
      // this.activeTab = 0
      this.setData({
        activeTab: 0,
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    console.log(e)
    const { from } = e
    const share = e.target.dataset.share
    let  imageUrl = 'https://ustatic.hudongmiao.com/joymewApp/share/nationDay@v2.png'
    if(from === 'button'&&share!="page"){
      const share_invite_key = wx.getStorageSync('invite_key');
      let path = '/pages/regist/regist';
      if (share_invite_key) {
        path = `${path}?share_invite_key=${share_invite_key}`
  
      }
      // console.log(path,share_invite_key )
      return {
        title: `注册并参与嗨喵巅峰赛 赢苹果14 PRO`,
        path,
        imageUrl
      }
    }
    else{
      const path = '/pages/activity/nationalDay/nationalDay'
      return {
        title: `参与嗨喵巅峰赛 赢苹果14 PRO`,
        path,
        imageUrl
      }
    }
  },
  onLoad(options) {

    // this.judgeCanShare()
  },
  
  changeTabFromChildren(e){
    // console.log(e)
    const num = Number(e.detail)
    this.changeTab(num)
  },
  changeTabTo1(newVal){
    console.log(newVal, typeof newVal)
    if(newVal===0){
      this.setData({
        toView: 'tab1',
        // jsScrolling: true
      })    
    }
    if(newVal===1){
      this.setData({
        toView: 'tab2',
        // jsScrolling: true
      })    
    }
  },
  changeTab(newVal){
    console.log(newVal, typeof newVal)
    // if(newVal === this.activeTab){
    //   return ;
    // }
    if(newVal===0){
      this.setData({
        toView: 'tab1',
        // jsScrolling: true
      })    
    }
    if(newVal===1){
      this.setData({
        toView: 'tab2',
        // jsScrolling: true
      })    
    }
  },
  onReady() {
    // const {
    //   optionsTab
    // } = this.data
    // if (optionsTab) {
    //   // this.optionsTab = options.tab
    //   // this.activeTab = Number(options.tab)
    //   this.changeTabTo1(1)
    // } else {
    //   const date = new Date();
    //   const month = date.getMonth() + 1
    //   const day = date.getDate()
    //   if (month === 10 && day > 7) {
    //     // this.activeTab = 1;
    //     this.changeTabTo1(1)
    //     console.log('已经到第二个活动时间了')
    //   }
    // }
    const query = wx.createSelectorQuery().in(this);
    query.select('#tab2').boundingClientRect(data => {
      console.log("得到布局位置信息" + JSON.stringify(data));
      console.log("节点离页面顶部的距离为" + data.top);

      this.setData({
        tabTop: data.top
      })
    }).exec();
  },
  input(e) {
    const index = e.detail
    this.setData({
      activeTab: Number(index)
    })
  },
  showRule() {
    this.setData({
      ruleDialogVisible: true
    })
  },
  showAward() {
    this.setData({
      awardDialogVisible: true
    })
  },
  close(e) {
    this.setData({
      ...e.detail
    })
  },
  initOctoberRank() {
    takeOctoberRank().then(res => {
      const {
        result1,
        user,
        is_user_hm
      } = res.data
      const {
        rankList,
        rankInfo
      } = result1
      let params = {}
      let list = rankList
      params.rankList = list || []
      params.canShowMore = is_user_hm == '1';
      params.myPersonalData2 = this.data.myPersonalData2
      if (user) {
        const {
          invite_key,
          emcee_name,
          real_name,
          avator,
          // end_like,

        } = user
        params.myPersonalData2 = {
          ...params.myPersonalData2,
          // end_like,
          invite_key,
          emcee_name,
          real_name,
          avator,

        }

      }
      if (rankInfo) {
        const {
          gap_like,
          end_like,
          size,
          lottery_money,
        } = rankInfo
        params.myPersonalData2 = {
          ...params.myPersonalData2,
          gap_like,
          end_like,
          size,
          lottery_money,
        }
      }

      this.setData(params)

    })
  },
  addPageMore() {
    wx.showToast({
      title: '下载嗨喵APP， 查看更多名次',
      icon: 'none'
    })
    return;
    const {
      canShowMore,
      showCount,
      rankList
    } = this.data
    if (!canShowMore) {
      wx.showToast({
        title: '活动期间暂无有效活动，完成活动可查看更多名次。',
        icon: 'none'
      })
      return;
    }
    if (showCount > rankList.length) {
      wx.showToast({
        title: '没有更多了',
        icon: 'none'
      })
      return;
    }
    this.setData({
      showCount:  showCount + 20
    })
  },
  getMoreRankList() {
    // console.log('more')
    this.setData({
      showCount: this.data.showCount + 10
    })
  },
  excuteTask(e) {
    const isTest = getApp().globalData.isInNationalTest
    const today = new Date()
    const month = today.getMonth() + 1
    if(month!==10 && !isTest){
      wx.showToast({
        icon: 'none',
        title: '活动尚未开始，请稍后再试',
      })
      return ;
    }
    const action = e.detail
    if(!action){
      wx.showToast({
        title: '活动错误',
        icon: 'none'
      })
      return 
    }
    switch (action) {
      case 'SignIn':
        this.setData({
          signDialogVisible: true
        })
        break;
      case 'complete_splid':
        wx.navigateTo({
          url: '/pages/index/applyActivity/applyActivity',
        })
        break;
      case 'Flow_Hd':

        break;
      case 'wawa_pay':
        wx.showToast({
          title: '请前往嗨喵APP分享',
          icon: 'none',
        })
        break;
      case 'save_schedule':
        wx.navigateTo({
          url: '/pages/calendar/personalDetail/p',
        })
        break;
      case 'inviter_join':
        // this.toShare()
        break;
      case 'inviter_JiHuo':
        wx.navigateTo({
          url: '/pages/activity/nationalDay/activeFriend/activeFriend'
        })
        break;
      default:
        console.log(action);
        break;

    }
  },
  initFlowInfo() {

    getFlowInfo().then(res => {
      const {
        list1,
        list2,
        list3,
        list4,
        myInfo,
        maxInfo
      } = res.data
      let params = {}
      params.maxInfo = maxInfo || {}
      params.myInfo = myInfo || {}
      params.myPersonalData1 = this.data.myPersonalData1

      params.awardObj = {
        list1: list1||[],
        list2: list2||[],
        list3: list3||[],
        list4: list4||[],
        maxInfo: params.maxInfo
      }

      params.myPersonalData1 = {
        ...params.myPersonalData1,
        maxInfo: params.maxInfo,
        ...params.myInfo,
      }
      this.setData(params)

    }).catch(err => {
      console.log(err)
    })
  },

  update(e) {
    if(!e && !e.detail){
      return ;
    }
    let target = e.detail
    this.setData(target)
  },
})